package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwDepositPlatformAccount;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwDepositPlatformAccountService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tcwDepositPlatformAccount")
public class ApiInterTcwDepositPlatformAccountController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwDepositPlatformAccountController.class);

    @Autowired
    private CommonApiService<TcwDepositPlatformAccount> commonApiService;

    @Autowired
    private TcwDepositPlatformAccountService tcwDepositPlatformAccountService;


    @Autowired
    private YspTaskService yspTaskService;
    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TcwDepositPlatformAccount setSeachSql(TcwDepositPlatformAccount cond){


        cond = commonApiService.dataAuth(MenuId.TCW_DepositPlatformAccount, cond);
        return  cond;
    }



    @RequiresPermissions("tcwDepositPlatformAccount:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TcwDepositPlatformAccount cond){

        cond.setOrderby("A.createTime DESC")
//            .addField("B.Code AS mainAccountCode")
//            .addField("B.PlatformName AS name")
//            .addField("B.Name AS mainAccountName")
//            .addField("B.CompanyVid AS mainAccountMid")
//            .addField("C.Vname AS affiliatedCompany")
//            .addField("D.Code AS regionName")
//            .addField("E.Name AS platformName")
//            .addField("D.PlatformCode AS platformCode")
//            .addField("D.Name AS regionNamejc")
//            .addField("F.Code AS currency")
//            .addField("F.Name AS currencyName")
//            .addField("G.VName AS vgsjc")
//            .addField("G.Vid AS vgsbm")
                .addLeftJoin("TCW_DepositPlatform B ON B.Code = A.DepositPlatformCode")
                .addLeftJoin("TB_CompanyInfo C ON C.Vid = B.CompanyVid")
                .addLeftJoin("TP_AmazonRegion D ON D.Code = A.RegionCode")
                .addLeftJoin("TP_AmazonShopExchangeRate E ON E.Code = A.CurrencyCode")
                .addLeftJoin("THR_OperateCompanyApply F ON F.Vgsbm = A.Vgsbm ")
                .addLeftJoin("TB_Platform G ON G.Code = D.PlatformCode")
                .addField("B.Name AS depositPlatformName")
                .addField("B.Code AS depositPlatformCode")
                .addField("B.PlatformName AS name")
                .addField("B.Mid AS depositPlatformMid")
                .addField("C.Vname AS companyVname")
                .addField("D.Code AS regionCode")
                .addField("G.Name AS platformName")
                .addField("G.Code AS platformCode")
                .addField("D.Name AS regionNamejc")
                .addField("E.Name AS currencyName")
                .addField("E.id AS currency")
                .addField("F.Vgsbm AS vgsbm")
                .addField("F.Vgsjc AS vgsjc")
        ;


        return commonApiService.searchLay(cond);
    }



    /**
     * 新增
     */
    @RequestMapping("/add")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    		) {
        try {
            logger.info("新增传参：" + params);
            //修改
            return tcwDepositPlatformAccountService.add(params);
        } catch (Exception e) {
            logger.error("新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("修改传参：" + params);
            //修改
            return tcwDepositPlatformAccountService.edit(params);
        } catch (Exception e) {
            logger.error("修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwDepositPlatformAccount cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //查询条件
            String nameLinked = ConditionUtil.getConditionValue(conditions, "_ys_createUserName");//制单人
            if (nameLinked != null){
                cond.where("U.UserId LIKE '%" + nameLinked + "%'" + " OR  U.UserName LIKE '%" + nameLinked + "%'");
            }
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.TCW_DepositPlatformAccount)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwDepositPlatformAccount> util = new ExportExcelUtils<>(new TcwDepositPlatformAccount());
            String toFilePath = util.exportExcel(listData, listField, "收款平台存款账户管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }



    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute TcwDepositPlatformAccount cond){
    	
    	cond.addLeftJoin("TCW_DepositPlatform B ON B.Code = A.DepositPlatformCode")
    		.addField("B.Name depositPlatformName")
    		.addLeftJoin("TB_CompanyInfo B1 ON B1.Vid = B.CompanyVid")
    		.addField("B1.Vid companyVid")
    		.addField("B1.Vname companyVname");
        return commonApiService.searchLay(cond);
    }
}
